package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yx.cloud.R;
import com.yx.cloud.bean.FurnitureImageBean;
import java.util.List;


public class FurnitureImageListAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private Context context;
    private List<FurnitureImageBean> list;
    private InnerItemOnclickListener listener;

    public FurnitureImageListAdapter(Context context, List<FurnitureImageBean> images) {
        this.context = context;
        this.list = images;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View furnitureImage = LayoutInflater.from(context).inflate(R.layout.furniture_imagelist_item,parent,false);
        FurnitureViewHolder viewHolder = new FurnitureViewHolder(furnitureImage);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        FurnitureImageBean furnitureImageBean = list.get(position);
        FurnitureViewHolder furnitureViewHolder= (FurnitureViewHolder) holder;
        furnitureViewHolder.tvFurnitureName.setText(furnitureImageBean.getName());
        furnitureViewHolder.ivFurnitureImage.setImageResource(furnitureImageBean.getImageId());
        furnitureViewHolder.ivFurnitureImage.setTag(position);
        furnitureViewHolder.ivFurnitureImage.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        listener.itemClick(v,(int)v.getTag());
    }

    public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener) {
        this.listener = listener;
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v,int position);
    }

    //创建布局FurnitureViewHolder
    class FurnitureViewHolder extends RecyclerView.ViewHolder{
        ImageView ivFurnitureImage;
        TextView tvFurnitureName;
        public FurnitureViewHolder(View itemView) {
            super(itemView);
            ivFurnitureImage = itemView.findViewById(R.id.iv_furniture_image);
            tvFurnitureName = itemView.findViewById(R.id.tv_furniture_name);
        }
    }
}

